package com.wnhoo.smartfishtank.ui;


import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wnhoo.smartfishtank.R;


/**
 *
 *
 */
public class ViewSearchDevice extends RelativeLayout {


//    private final String tips_string_doing = "正在搜索智能蓝牙RGB灯产品\n请确认RGB灯已经接入电源";
//    private final String tips_string_nothing = "没有搜索到智能RGB灯产品";
    private  String tips_string_doing ;
    private  String tips_string_nothing;
    private TextView label;
    private ImageView image_view_1;
    private ImageView image_view_2;
    private ImageView image_view_3;
    private ImageView image_view_4;
    private ImageView image_view_5;
    private ImageView image_view_center;
    public boolean isStopAnim = false;
    //放大动画的时间
    private final long play_time = 3600;

    //动画结束之后，还原的时间
    private final long play_time_over = 10;

    //每隔以个动画之间的时间间隔,延时
    private long sleep_time = play_time/4;


    private Context context;

    private OnClickListener listener;

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public ViewSearchDevice(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub

        this.context = context;
        initView();
    }

    /**
     * @param context
     * @param attrs
     */
    public ViewSearchDevice(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        tips_string_doing = context.getResources().getString(R.string.scan_ing);
        tips_string_nothing = context.getResources().getString(R.string.scan_no_device);
        initView();
    }

    /**
     * @param context
     */
    public ViewSearchDevice(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
        initView();
    }

    public void initView(){
        Resources resources = getResources();
        LayoutParams p_wrap = new LayoutParams(resources.getDimensionPixelOffset(R.dimen.scan_view_width),
                resources.getDimensionPixelOffset(R.dimen.scan_view_heiht));
                //new LayoutParams(-2, RelativeLayout.LayoutParams.WRAP_CONTENT);
        p_wrap.addRule(RelativeLayout.CENTER_IN_PARENT);

        //Tips Label
        label = new TextView(context);
        label.setGravity(Gravity.CENTER);
        label.setTextColor(resources.getColor(R.color.ther_text_color));
        //label.setTextSize(14);//sp
        label.setTextSize(TypedValue.COMPLEX_UNIT_PX,resources.getDimensionPixelOffset(R.dimen.size_label));//px
        label.setText(tips_string_doing);
        LayoutParams p_tv = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        p_tv.addRule(RelativeLayout.CENTER_HORIZONTAL);
        p_tv.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        p_tv.setMargins(0,0,0,resources.getDimensionPixelOffset(R.dimen.header_height));
        label.setLayoutParams(p_tv);

        image_view_1 = new ImageView(context);
        image_view_1.setLayoutParams(p_wrap);
        image_view_1.setBackgroundResource(R.drawable.scan_circle2_white);

        image_view_2 = new ImageView(context);
        image_view_2.setLayoutParams(p_wrap);
        image_view_2.setBackgroundResource(R.drawable.scan_circle2_white);

        image_view_3 = new ImageView(context);
        image_view_3.setLayoutParams(p_wrap);
        image_view_3.setBackgroundResource(R.drawable.scan_circle2_white);

        image_view_4 = new ImageView(context);
        image_view_4.setLayoutParams(p_wrap);
        image_view_4.setBackgroundResource(R.drawable.scan_circle2_white);

        image_view_center = new ImageView(context);
//        LayoutParams p_1 =
//        p_1.addRule(RelativeLayout.CENTER_IN_PARENT);
        image_view_center.setLayoutParams(p_wrap);
        image_view_center.setBackgroundResource(R.drawable.scan_icon_doing);

        image_view_1.setVisibility(View.INVISIBLE);
        image_view_2.setVisibility(View.INVISIBLE);
        image_view_3.setVisibility(View.INVISIBLE);
        image_view_4.setVisibility(View.INVISIBLE);

        this.addView(image_view_1);
        this.addView(image_view_2);
        this.addView(image_view_3);
        this.addView(image_view_4);
        this.addView(image_view_center);
        this.addView(label);

        startAnimator();
    }

    public void setViewBackgroundResource(int resource){

        image_view_1.setBackgroundResource(resource);
        image_view_2.setBackgroundResource(resource);
        image_view_3.setBackgroundResource(resource);
        image_view_4.setBackgroundResource(resource);
    }

    public void startAnimator(){

        isStopAnim = false;
        play(image_view_1);
        new Thread(){

            public void run() {

                try {
                    if(isStopAnim){
                        this.interrupt();
                    }
                    sleep(sleep_time);
                    sendMessageToUI(2,null);
                    sleep(sleep_time);
                    sendMessageToUI(3,null);
                    sleep(sleep_time);
                    sendMessageToUI(4,null);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally {
                        this.interrupt();
                }

            };
        }.start();

    }

    public void setOnClickRefreshListener(OnClickListener l){

        listener = l;
        image_view_center.setOnClickListener(l);
    }

    public void setStatusDoing(){

        showAnimator();
        image_view_center.setBackgroundResource(R.drawable.scan_icon_doing);
        label.setText(tips_string_doing);
    }
    public void setStatusNothing(){

        hideAnimator();
        image_view_center.setBackgroundResource(R.drawable.scan_icon_nothing);
        label.setText(tips_string_nothing);
    }
    /**
     * 无效果
     */
    public void hideAnimator(){


        System.out.println("stopAnimator");
        isStopAnim = true;
        image_view_1.clearAnimation();
        image_view_2.clearAnimation();
        image_view_3.clearAnimation();
        image_view_4.clearAnimation();
        //image_view_5.clearAnimation();



//        this.removeAllViewsInLayout();
//
//        image_view_center.setOnClickListener(this.listener);
//        this.addView(image_view_center);

//        this.removeViewInLayout(image_view_1);
//        this.removeViewInLayout(image_view_2);
//        this.removeViewInLayout(image_view_3);
//        this.removeViewInLayout(image_view_4);

        image_view_1.setVisibility(View.INVISIBLE);
        image_view_2.setVisibility(View.INVISIBLE);
        image_view_3.setVisibility(View.INVISIBLE);
        image_view_4.setVisibility(View.INVISIBLE);
    }

    public void showAnimator(){

        System.out.println("showAnimator");
        isStopAnim = false;

//        image_view_1.invalidate();
//        image_view_2.invalidate();
//        image_view_3.invalidate();
//        image_view_4.invalidate();
//
//        this.addView(image_view_1);
//        this.addView(image_view_2);
//        this.addView(image_view_3);
//        this.addView(image_view_4);
//
//        startAnimator();
        image_view_1.setVisibility(View.VISIBLE);
        image_view_2.setVisibility(View.VISIBLE);
        image_view_3.setVisibility(View.VISIBLE);
        image_view_4.setVisibility(View.VISIBLE);
    }
    public void sendMessageToUI(int what ,Object o){

        Message ms = Message.obtain();
        ms.what = what;
        ms.obj = o;
        handler.sendMessage(ms);

    }

    protected Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    play(image_view_1);
                    break;
                case 2:
                    play(image_view_2);
                    break;
                case 3:
                    play(image_view_3);
                    break;
                case 4:
                    play(image_view_4);
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        };

    };

    public void play(ImageView image_view){

        image_view.setVisibility(View.VISIBLE);

        AnimatorSet set_01 = new AnimatorSet() ;
        AnimatorSet set_02 = new AnimatorSet() ;

        ObjectAnimator anim1 = ObjectAnimator .ofFloat(image_view, "scaleX", 1f,2f);
        anim1.setDuration(play_time);

        ObjectAnimator anim2 = ObjectAnimator .ofFloat(image_view, "scaleX", 2f,1f);
        anim2.setDuration(play_time_over);

        ObjectAnimator anim3 = ObjectAnimator .ofFloat(image_view, "scaleY", 1f,2f);
        anim3.setDuration(play_time);

        ObjectAnimator anim4 = ObjectAnimator .ofFloat(image_view, "scaleY", 2f,1f);
        anim4.setDuration(play_time_over);

        ObjectAnimator anim_alpha = ObjectAnimator.ofFloat(image_view, "alpha", 0.6f, 0f);
        anim_alpha.setDuration(play_time);

        ObjectAnimator anim_alpha_0 = ObjectAnimator.ofFloat(image_view, "alpha", 0f, 0.6f);
        anim_alpha_0.setDuration(play_time_over);

        set_01.play(anim1).with(anim3);
        set_01.play(anim_alpha).before(set_02) ;

        set_02.play(anim2).with(anim4);
        set_02.play(anim_alpha_0).before(set_01);


        set_01.start();
    }


}
